package com.ebs.md.fxmonitor.marketmonitor.config;

/**
 * Revision Author:
 *  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/md/fxmonitor/marketmonitor/config/SelfMatchingConfigImpl.java $
 *  $Revision: #9 $
 *  $DateTime: 2012/08/12 10:07:06 $
 *  $Change: 113123 $
 *  $Author: Arik.Sher $
 */

import org.apache.log4j.Logger;

import com.ebs.md.fxmonitor.marketmonitor.MarketMonitorConsts;
import com.ebsdr.fsp.ccc.configuration.IConfiguration;


public class SelfMatchingConfigImpl implements ISelfMatchingConfig, IConfigListener, MarketMonitorConsts
{
    final protected Logger logger = Logger.getLogger(getClass());
    final protected static String xsdPath = "/MonitoredMarketActivity/SelfMatchings/SelfMatching";
	
	private EAggressiveOrderType aggressiveOrderType;
	private boolean              isAggressiveVsPassiveOrderSizeEquals;
	private int                  selfMatchThreshold;
	private long                 timeToMatchThreshold;
	
	
	@Override
	public String getName() {
		return BASIC_CONFIG_NAME;
	}

	@Override
	public void onConfig(IConfiguration configuration) 
	{
		String aggressiveOrderTypeStr          = configuration.getString(xsdPath + "/@aggressiveOrderType",          EVENT_SELF_MATCHING_AGGRESSIVE_ORDER_TYPE);
		String aggressiveVsPassiveOrderSizeStr = configuration.getString(xsdPath + "/@aggressiveVsPassiveOrderSizeaggressiveVsPassiveOrderSize", EVENT_SELF_MATCHING_AGGRESSIVE_VS_PASSIVE_ORDER_SIZE_TYPE);
		selfMatchThreshold                     = configuration.getInt   (xsdPath + "/@selfMatchThreshold",           EVENT_SELF_MATCHING_SELF_MATCH_THRESHOLD);
		timeToMatchThreshold                   = configuration.getLong  (xsdPath + "/@timeToMatchThreshold",         EVENT_SELF_MATCHING_TIME_TO_MATCH_THRESHOLD);
		
		aggressiveOrderType                  = setAggressiveOrderType(aggressiveOrderTypeStr);
		isAggressiveVsPassiveOrderSizeEquals = setAggressiveVsPassiveOrderSize(aggressiveVsPassiveOrderSizeStr);
		
		logger.info( getConfigInfo() );
	}

	@Override
	public String getConfigInfo() {
		StringBuilder sb = new StringBuilder("SelfMatchingConfigImpl: ");
		
		sb.append( " aggressiveOrderType: "          + aggressiveOrderType);
		sb.append(", aggressiveVsPassiveOrderSize: " + isAggressiveVsPassiveOrderSizeEquals);
		sb.append(", selfMatchThreshold: "           + selfMatchThreshold);
		sb.append(", timeToMatchThreshold: "         + timeToMatchThreshold);
		
		return sb.toString();
	}

	private boolean setAggressiveVsPassiveOrderSize(String aggressiveVsPassiveOrderSizeStr) {
		boolean isEqual = true;
		
		if (aggressiveVsPassiveOrderSizeStr != null && aggressiveVsPassiveOrderSizeStr.equals("NOT EQUAL")){
			isEqual = false;
		}
		
		return isEqual;
	}

	private EAggressiveOrderType setAggressiveOrderType(String aggressiveOrderTypeStr) {
		EAggressiveOrderType res = EAggressiveOrderType.IOC;
		
		if (aggressiveOrderTypeStr != null && aggressiveOrderTypeStr.equals("ANY")){
			res = EAggressiveOrderType.ANY;
		}

		return res;
	}

	@Override
	public EAggressiveOrderType getAggressiveOrderType() {
		return aggressiveOrderType;
	}

	@Override
	public boolean getIsAggressiveVsPassiveOrderSizeEquals() {
		return isAggressiveVsPassiveOrderSizeEquals;
	}

	@Override
	public int getSelfMatchThreshold() {
		return selfMatchThreshold;
	}

	@Override
	public long getTimeToMatchThreshold() {
		return timeToMatchThreshold;
	}

}
